package javabase.schoolwork.day6_10;

/**
 * @author fance
 * @date 2018/6/10 14:09
 */



public class OneToBIg {
    private static Object lock = new Object();
    private static int i = 1;
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (i < 1000) {
                    synchronized (lock) {
                        System.out.println("Thread1: "+i++);
                        lock.notifyAll();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (i < 1000) {
                    synchronized (lock) {
                        System.out.println("Thread2: "+i++);
                        lock.notifyAll();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });


        thread1.start();
        thread2.start();
    }

}

